package com.ryenlii.datatrans.adapter;

import cn.hutool.log.StaticLog;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public abstract class AbstractRequestSend {

    /**
     * 模拟异步执行任务
     */
    private final ExecutorService executorService = Executors.newSingleThreadExecutor();

    public RequestFuture send(String message) {
        // 这里是Future模式
        RequestFuture requestFuture = new RequestFuture();
        executorService.execute(new AsyncExeRemoteInvoke(requestFuture, message));
        return requestFuture;
    }

    private static class AsyncExeRemoteInvoke implements Runnable {
        private final RequestFuture requestFuture;
        private final String response;

        public AsyncExeRemoteInvoke(RequestFuture requestFuture, String message) {
            this.requestFuture = requestFuture;
            this.response = message;
        }

        @Override
        public void run() {
            // 模拟远程调用耗时
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                StaticLog.error(e);
            }
            String res = response + "远程调用结束";
            requestFuture.onComplete(res);
        }
    }
}